<template>
    <div class="medical-records-info-container">
        <info-head v-model="infoData" @getData="getData" @searchData="searchData" @getDRG="getDRG" @finishCheck="finishCheck"/>
        <div class="info-table-group">
            <info-table v-model="infoData" @getHelper="getHelper" @searchTip="searchTip"/>
        </div>
        <info-tool v-model="infoData" ref="infoTool" @getData="getListBah"/>
    </div>
</template>

<script>
    import {
        getDRGData,
        MRTableData,
        MRQCData,
        getQCMenuData,
        MRQCAuditData,
        MRQCCollectData,
        getMRState
    } from '@/api/medical-records/medical-records-info'
    import InfoHead from '@/views/medical-records/medical-records-info/components/InfoHead'
    import InfoTable from '@/views/medical-records/medical-records-info/components/InfoTable'
    import InfoTool from '@/views/medical-records/medical-records-info/components/InfoTool'

    export default {
        name: 'RecordsInfo',
        data () {
            return {
                // 所有数据
                infoData: {
                    noneCodingError: [],
                    noneCodingWaring: [],
                    codingError: [],
                    codingWaring: [],
                    allDeduction: [],
                    DRGsData: {},
                    resExamine: []
                },
                // 使用储存的数据，不去请求
                useStore: false
            }
        },
        created () {
            // tags判断入口是否从选项卡进入
            this.useStore = this.$route.query.tags === 'tags'
            if (sessionStorage.getItem(`${this.$route.name}QCInfo`) && this.useStore) {
                let sessionData = JSON.parse(sessionStorage.getItem(`${this.$route.name}QCInfo`))
                if (sessionData !== {}) {
                    this.infoData = sessionData
                } else {
                    this.getListBah()
                }
            } else {
                this.getListBah()
            }
        },
        mounted () {
            document.onkeypress = this.doKey()
            document.onkeydown = this.doKey()
        },
        methods: {
            /**
             * 阻止回退
             */
            doKey (e) {
                let ev = e || window.event
                if (ev) {
                    let obj = ev.target || ev.srcElement
                    let t = obj.type || obj.getAttribute('type')
                    if (ev.keyCode === 8 && t !== 'password' && t !== 'text' && t !== 'textarea') {
                        return false
                    }
                    if (ev.keyCode === 13) {
                        return false
                    }
                }
            },
            /**
             * 搜索病案号
             * @param bah 病案号
             */
            searchData (bah) {
                this.setGetStore(this.$route.name, {keyword: bah})
                this.getListBah()
            },
            /**
             * 翻页
             */
            getData (index) {
                this.setGetStore(this.$route.name, {pageIndex: index})
                this.getListBah()
            },
            /**
             * 获取列表病案号
             */
            getListBah () {
                let params = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                params.pageSize = 1
                if (this.$route.name === 'QCEffectInfo') {
                    this.getQCMenu(params)
                } else if (this.$route.name === 'MyMRAuditInfo') {
                    this.getQCAuditDataMRN(params)
                } else if (this.$route.name === 'MyMRCollectInfo') {
                    this.getQCCollectDataMRN(params)
                } else {
                    this.getOverviewDataMRN(params)
                }
            },
            /**
             * 质控效果分析的数据
             * @param params 请求参数
             */
            getQCMenu (params) {
                getQCMenuData(params).then(res => {
                    if (res.code.toString() === '200') {
                        if (res.data.rows[0].medicalRecordID) {
                            this.infoData = {
                                ...this.infoData,
                                ...res.data.rows[0],
                                index: res.data.index,
                                count: res.data.total
                            }
                            let query = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                            let data = {
                                medicalRecordSign: this.infoData.medicalRecordSign,
                                hospitalTime: res.data.rows[0].hospitalTime,
                                isLast: params.isLast,
                                orgId: query.orgId
                            }
                            this.getMRData(data)
                            this.getDRG(data)
                            this.getDataState(this.infoData.medicalRecordID)
                            this.getHelper()
                        } else {
                            this.$message.error('当前病案号不存在，请重新输入')
                        }
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            /**
             * 审核列表的数据
             * @param params 请求参数
             */
            getQCAuditDataMRN (params) {
                MRQCAuditData(params).then(res => {
                    if (res.code.toString() === '200') {
                        if (res.data.rows[0].medicalRecordID) {
                            this.infoData = {
                                ...this.infoData,
                                ...res.data.rows[0],
                                index: res.data.index,
                                count: res.data.total
                            }
                            let query = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                            let data = {
                                medicalRecordSign: this.infoData.medicalRecordSign,
                                hospitalTime: res.data.rows[0].hospitalTime,
                                isLast: params.isLast,
                                orgId: query.orgId
                            }
                            this.getMRData(data)
                            this.getDRG(data)
                            this.getDataState(this.infoData.medicalRecordID)
                            this.getHelper()
                        } else {
                            this.$message.error('当前病案号不存在，请重新输入')
                        }
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            /**
             * 收藏列表的数据
             * @param params 请求参数
             */
            getQCCollectDataMRN (params) {
                MRQCCollectData(params).then(res => {
                    if (res.code.toString() === '200') {
                        if (res.data.rows[0].medicalRecordID) {
                            this.infoData = {
                                ...this.infoData,
                                ...res.data.rows[0],
                                index: res.data.index,
                                count: res.data.total
                            }
                            let query = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                            let data = {
                                medicalRecordSign: this.infoData.medicalRecordSign,
                                hospitalTime: res.data.rows[0].hospitalTime,
                                isLast: params.isLast,
                                orgId: query.orgId
                            }
                            this.getMRData(data)
                            this.getDRG(data)
                            this.getDataState(this.infoData.medicalRecordID)
                            this.getHelper()
                        } else {
                            this.$message.error('当前病案号不存在，请重新输入')
                        }
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            /**
             * 获取总览数据(病案号)
             * @param params 请求参数
             */
            getOverviewDataMRN (params) {
                MRQCData(params).then(res => {
                    if (res.code.toString() === '200') {
                        if (res.data.rows[0].medicalRecordID) {
                            this.infoData = {
                                ...this.infoData,
                                ...res.data.rows[0],
                                index: res.data.index,
                                count: res.data.total
                            }
                            let query = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                            let data = {
                                medicalRecordSign: this.infoData.medicalRecordSign,
                                hospitalTime: res.data.rows[0].hospitalTime,
                                isLast: params.isLast,
                                orgId: query.orgId
                            }
                            this.getMRData(data)
                            this.getDRG(data)
                            this.getDataState(this.infoData.medicalRecordID)
                            this.getHelper()
                        } else {
                            this.$message.error('当前病案号不存在，请重新输入')
                        }
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            /**
             * 根据病案id查数据
             * @param params 请求参数
             */
            getMRData (params) {
                MRTableData(params).then(res => {
                    if (res.code.toString() === '200') {
                        this.infoData = {
                            ...this.infoData,
                            ...JSON.parse(res.data)
                        }
                        this.useStore = false
                        sessionStorage.setItem(`${this.$route.name}QCInfo`, JSON.stringify(this.infoData))
                    }
                })
            },
            /**
             * 审核后执行
             */
            finishCheck (data, id) {
                this.setGetStore(this.$route.name, {pageIndex: this.infoData.index})
                this.getMRData(data)
                this.getDRG(data)
                this.getDataState(id)
                this.getHelper()
            },
            /**
             * 获取小助手数据
             */
            getHelper () {
                this.$nextTick(() => {
                    this.$refs.infoTool.getHelperData()
                })
            },
            /**
             * 点击错误信息查询ICD
             * @param data 查询内容
             */
            searchTip (data) {
                this.$refs.infoTool.searchTipICD(data)
            },
            /**
             * 获取DRG信息
             * @param params 请求参数
             */
            getDRG (params) {
                this.infoData.DRGsData = {
                    name: '',
                    code: '',
                    weight: '',
                    drgIncome: '',
                    lowMagnification: '',
                    highMagnification: ''
                }
                if (JSON.parse(localStorage.getItem('rolesCode')).includes(200000000000)) {
                    getDRGData(params).then(res => {
                        if (res.code.toString() === '200') {
                            if (res.data) {
                                this.infoData.DRGsData = res.data
                                this.useStore = false
                                sessionStorage.setItem(`${this.$route.name}QCInfo`, JSON.stringify(this.infoData))
                            } else {
                                this.infoData.DRGsData = res.data
                            }
                        }
                    })
                }
            },
            /**
             * 获取病案审核记录和收藏
             * @param medicalRecordID 病案号
             */
            getDataState (medicalRecordID) {
                let params = {
                    medicalRecordID
                }
                getMRState(params).then(res => {
                    if (res.code.toString() === '200') {
                        this.infoData.resExamine = res.data.resExamine
                        this.infoData.isCollection = res.data.isCollection
                        this.useStore = false
                        sessionStorage.setItem(`${this.$route.name}QCInfo`, JSON.stringify(this.infoData))
                    }
                })
            }
        },
        components: {
            InfoHead,
            InfoTable,
            InfoTool
        }
    }
</script>

<style lang="scss" scoped>
    .medical-records-info-container {
        background: #fff;
        margin-top: 10px;
        padding: 5px 10px;
        position: relative;
        .info-table-group {
            height: calc(100vh - 60px - 30px - 30px - 10px - 5px - 50px - 20px);
            overflow-y: auto;
        }
    }
</style>
